package com.example.sample.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;

import com.example.app.BaseActivity;
import com.example.sample.R;

import net.liteheaven.diagnose.DiagnoseController;
import net.liteheaven.diagnose.DiagnoseOutputListener;
import net.liteheaven.diagnose.DiagnoseStateListener;
import net.liteheaven.xblib.common.log.XLog;
import net.liteheaven.xblib.common.ui.ToastUtil;
import net.liteheaven.xblib.widget.edittext.FixedLineCountEditText;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by LiangYu on 2017/6/2.
 */
public class NetworkDoctorActivity extends BaseActivity {

    private FixedLineCountEditText infoEdit;
    private Button reportSuccess;
    private Button reportFailure;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_network_doctor);
        infoEdit = (FixedLineCountEditText)findViewById(R.id.infoEdit);
        infoEdit.setMaxLineCount(20);
        reportSuccess = (Button)findViewById(R.id.reportSuccess);
        reportSuccess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DiagnoseController.inst().reportRequestResult(true);
            }
        });
        reportFailure = (Button)findViewById(R.id.reportFailure);
        reportFailure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DiagnoseController.inst().reportRequestResult(false);
            }
        });

        initNetworkDoctor();
        pingByJava();

        // 测试打印异常
        String a = "1\n2\n3\n4";
        System.out.println(a);
    }

    private void initNetworkDoctor(){
        DiagnoseController.inst().setOutputListener(new DiagnoseOutputListener() {
            @Override
            public void onOutput(String s) {
                infoEdit.appendLine(s);
            }
        });

        DiagnoseController.inst().setStateListener(new DiagnoseStateListener() {
            @Override
            public void onState(boolean state) {
                ToastUtil.show(NetworkDoctorActivity.this, "onState " + state);
            }
        });
    }

    /**
     * InetAddress#isReachable不靠谱，还是用bash ping吧
     */
    private void pingByJava(){
        e.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    InetAddress ia = InetAddress.getByName("192.168.10.1");
                    log("ip = " + ia.toString());
                    boolean isReachable = ia.isReachable(1000);
                    log("isReachable = " + isReachable);

                    Socket socket = new Socket();
                    SocketAddress socketAddress = new InetSocketAddress(ia, 80);
                    socket.connect(socketAddress, 1000);

                    log("connect ok");
                } catch (ConnectException e){
                    e.printStackTrace();
                } catch (java.io.IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void log(final String log){
        XLog.d(NetworkDoctorActivity.class.getSimpleName(), log);
    }

    private ExecutorService e = Executors.newFixedThreadPool(30);
}
